package org.example.strategy;

import java.util.LinkedList;

public class DeleteStrategy implements EditorStrategy{
    private String content;
    private LinkedList<String> fileContents;
    private Boolean isFileChanged = false;
    public DeleteStrategy(String content,LinkedList<String> fileContents){
        this.content = content;
        this.fileContents = fileContents;
    }

    public LinkedList<String> getFileContents(){
        return fileContents;
    }
    public Boolean getIsFileChanged(){
        return isFileChanged;
    }
    @Override
    public void performAction() {
        if (content.matches("\\d+") && !content.startsWith("0") && Integer.valueOf(content) <= fileContents.size()){
            fileContents.remove(fileContents.get(Integer.valueOf(content) - 1));
            isFileChanged = Boolean.TRUE;
        }else {
            for (int index = 0;index < fileContents.size();index++){
                String[] line = fileContents.get(index).trim().split(" ",2);
                for (String part:line){
                    if (part.equals(content)){
                        fileContents.remove(fileContents.get(index));
                        isFileChanged = Boolean.TRUE;
                    }
                }
            }
        }
    }
}
